/**
 * 
 */
package edu.arizona.bio5.onto.decomposition.persistent;

import java.util.Set;

import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLEntity;

import edu.arizona.bio5.onto.decomposition.AbstractAtom;
import edu.arizona.bio5.onto.decomposition.Atom;

/**
 * @author Pavel Klinov
 *
 */
public class PersistentAtom extends AbstractAtom implements Atom {

	private boolean m_loaded = false;
	private AtomLoader m_loader = null;
	
	public PersistentAtom(Object id, AtomLoader loader) {
		
		super(id);
		m_loader = loader;
	}

	
	public boolean isLoaded() {
		
		return m_loaded;
	}
	
	protected void setLoaded(boolean loaded) {
		
		m_loaded = loaded;
	}
	
	protected AtomLoader getLoader() {
		return m_loader;
	}
	
	@Override
	public Set<OWLEntity> getSignature() {
		
		if (!m_loaded) {
			
			try {
				
				loadAxioms();
				
			} catch (ADPersistenceException e) {
				// Shouldn't happen. If the atom has been created then the underlying file must exist and be OK.
				// This should be checked before we get here, here we can only throw a runtime exception
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		
		return super.getSignature();
	}

	@Override
	public Set<OWLAxiom> getAxioms() {

		if (!m_loaded) {
			
			try {
				
				loadAxioms();
				
			} catch (ADPersistenceException e) {

				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		
		return super.getAxioms();		
	}
	
	@Override
	public Set<OWLAxiom> getAllAxioms() {

		if (!m_loaded) {
			
			try {
				loadAxioms();
				
			} catch (ADPersistenceException e) {

				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		
		return super.getAllAxioms();		
	}	
	
	public void loadAxioms() throws ADPersistenceException {
		m_loader.load(this);
		m_loaded = true;
	}
	
	public void unloadAxioms() {		
		m_loaded = false;
		setAxiomSet(null);
		
	}

	@Override
	public void setAxiomSet(Set<OWLAxiom> axioms) {
		super.setAxiomSet(axioms);
	}


	@Override
	public void addNonLogicalAxiom(OWLAxiom ann) {
		throw new UnsupportedOperationException();
	}
}
